package com.ruoyi.dynamic.service;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.dynamic.mapper.RoomPasterMapper;
import com.ruoyi.dynamic.model.constant.DynamicConstant;
import com.ruoyi.dynamic.model.constant.RoomChatRoomKvConstant;
import com.ruoyi.dynamic.model.pojo.entity.Room;
import com.ruoyi.dynamic.model.pojo.entity.RoomPaster;
import com.ruoyi.dynamic.model.pojo.request.app.AppRoomAuctionStartRequest;
import com.ruoyi.dynamic.model.pojo.request.app.AppRoomPasterAddRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

/**
 * <p>
 * 房间贴纸 服务实现类
 * </p>
 *
 * @author Gnc
 * @since 2025-01-13
 */
@Service
public class RoomPasterServiceImpl extends ServiceImpl<RoomPasterMapper, RoomPaster> implements IRoomPasterService {

    @Autowired
    private IRoomService iRoomService;
    @Autowired
    private IMessageService iMessageService;


    @Override
    public void add(AppRoomPasterAddRequest request, Long userId) {
        Room verify = iRoomService.verify(request.getRoomId());
        RoomPaster paster = verify(request.getId());
        paster.setContent(request.getContent());
        paster.setStatus(DynamicConstant.CheckStatus.YES);

        iMessageService.setRoomChatRoomAttr(verify.getGroupId(), verify.getUserId().toString(),
                RoomChatRoomKvConstant.ROOM_PASTER_INFO, JSONObject.toJSONString(paster), 0, "", "");


    }

    @Override
    public RoomPaster verify(Long id) {
        RoomPaster roomPaster = getById(id);
        Assert.notNull(roomPaster, "此贴纸不存在");
        return roomPaster;
    }

    @Override
    public void updateLocation(AppRoomPasterAddRequest request, Long userId) {
        Room verify = iRoomService.verify(request.getRoomId());

        RoomPaster paster = verify(request.getId());
        paster.setContent(request.getContent());
        paster.setXLocation(request.getXLocation());
        paster.setYLocation(request.getYLocation());

        iMessageService.setRoomChatRoomAttr(verify.getGroupId(), verify.getUserId().toString(),
                RoomChatRoomKvConstant.ROOM_PASTER_INFO, JSONObject.toJSONString(paster), 0, "", "");
    }

    @Override
    public void stop(AppRoomAuctionStartRequest request, Long userId) {
        Room verify = iRoomService.verify(request.getRoomId());
        RoomPaster paster = verify(request.getId());
        paster.setStatus(DynamicConstant.CheckStatus.NO);
        iMessageService.setRoomChatRoomAttr(verify.getGroupId(), verify.getUserId().toString(),
                RoomChatRoomKvConstant.ROOM_PASTER_INFO, JSONObject.toJSONString(paster), 0, "", "");
    }
}
